/**
 * Created by Freeman on 2016/6/27.
 */

import cookie from 'react-cookie'
import {CookieDomain} from '../config'

let cookieConfig = {}
if (CookieDomain !== ''){
    cookieConfig = {domain:CookieDomain}
}

export function getCookie(name) {
    return cookie.load(name);
}

export function setCookie(name,value) {
    cookie.save(name,value,cookieConfig)
}

export function signOut() {
    cookie.remove('token',cookieConfig)
}

export function isLogin() {
    return !!cookie.load('token')
}

export function redirectToBack(nextState, replace) {
    //已经登录则不进入
    if (isLogin()) {
        replace('/')
    }
}
export function redirectToLogin(nextState,replace) {
    if (!isLogin()) {
        replace('/login')
    }
}